import 'dart:convert';
import '../services/Storage.dart';

Map info = {"username": 13266858126, "tel": 13266858126, "password": 123456999};

// 用户注册信息：保存、获取、删除、登录状态
class UserInfoServices {
  // 设置用户信息
  static setUserInfo(userInfoData) async {
    await Storage.setString('userInfo', json.encode(userInfoData));
  }

  // 获取用户信息
  static getUserInfo() async {
    try {
      List userInfoData = json.decode(await Storage.getString('userInfo'));
      return userInfoData;
    } catch (err) {
      return [];
    }
  }

  // 判断用户是否登录
  static getUserLoginState() async {
    List userInfo = await UserInfoServices.getUserInfo();
    return userInfo.length > 0 && userInfo[0]['username'] != '';
  }

  // 退出登录
  static loginOut() {
    Storage.remove('userInfo');
  }

  // 将用户地址保存到内存
  static setAddress(String address) async {
    await Storage.setString('address', address);
  }

  // 获取内存中的地址
  static getAddress() async {
    try {
      String addressData = await Storage.getString('address');
      return addressData;
    } catch (err) {
      return '';
    }
  }
}
